﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AbstraX.ClientInterfaces;
using System.Reflection;
using AbstraX.XPathBuilder;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using MvvmTreeView;
using AbstraX;

namespace TreeBuilderTest.Entities
{
    public class BasePropertyElement : IElement
    {
        private PropertyInfo property;
        private IBase parent;
        private string queryWhereProperty;
        private object queryWhereValue;

       public IAbstraXExtension LoadExtention(IBase baseObject, ITreeNode treeNode)
        {
            return null;
        }
        public BasePropertyElement(PropertyInfo property, IBase parent)
        {
            this.property = property;
            this.parent = parent;
        }

        public bool IsContainer
        {
            get 
            {
                return true;
            }
        }

        public IType DataType
        {
            get 
            {
                var type = new BaseType(property.PropertyType);

                // total hack - please remove

                if (property.PropertyType == typeof(object) && property.Name == "Image")
                {
                    type.IsImageType = true;
                }

                return type;
            }
        }

        public IEnumerable<IAttribute> Attributes
        {
            get 
            {
                return new List<IAttribute>();
            }
        }

        public IEnumerable<IOperation> Operations
        {
            get 
            {
                return new List<IOperation>();
            }
        }

        public event ChildrenLoadedHandler ChildrenLoaded;

        public void LoadChildren()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IElement> ChildElements
        {
            get 
            {
                var typeElement = new BaseTypeElement(property.PropertyType, this);

                if (typeElement.DataType.IsCollectionType)
                {
                    if (queryWhereProperty != null && queryWhereProperty == "Type")
                    {
                        yield return new BaseTypeElement(typeElement.DataType.CollectionType, this);
                    }
                    else
                    {
                        yield return new BaseTypeElement(typeElement.DataType.CollectionType, this);
                    }
                }
                else
                {
                    if (queryWhereProperty != null && queryWhereProperty == "Type")
                    {
                        yield return typeElement;
                    }
                    else
                    {
                        yield return typeElement;
                    }
                }
            }
        }

        public string ID
        {
            get 
            {
                return this.MakeID("Property='" + property.Name + "'");
            }
        }

        public string ParentID
        {
            get 
            {
                return this.Parent.ID;
            }
        }

        public string Name
        {
            get 
            {
                return property.Name;
            }
        }

        public string Description
        {
            get { throw new NotImplementedException(); }
        }

        public string Documentation
        {
            get { throw new NotImplementedException(); }
        }

        public string ImageURL
        {
            get { throw new NotImplementedException(); }
        }

        public IBase Parent
        {
            get
            {
                return parent;
            }
        }

        public string FolderKeyPair
        {
            get { throw new NotImplementedException(); }
        }

        public System.ServiceModel.DomainServices.Client.DomainContext Context
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void ExecuteWhere(string property, object value)
        {
            if (value is XPathAxisElement && property == "ID")
            {
                var predicate = ((XPathAxisElement)value).Predicates.First();

                queryWhereProperty = predicate.Left;
                queryWhereValue = predicate.Right;
            }
            else
            {
                queryWhereProperty = property;
                queryWhereValue = value;
            }
        }

        public void ExecuteWhere(System.Linq.Expressions.Expression expression)
        {
        }

        public void ExecuteWhere(XPathAxisElement element)
        {
        }

        public void ExecuteWhere(XPathAxisElement element, XPathAxisElement childrenElement)
        {
            var predicate = childrenElement.Predicates.First();

            queryWhereProperty = predicate.Left;
            queryWhereValue = predicate.Right;
        }

        public void ClearPredicates()
        {
            queryWhereProperty = null;
            queryWhereValue = null;
        }


        public DefinitionKind Kind
        {
            get { throw new NotImplementedException(); }
        }


        public bool HasChildren
        {
            get { throw new NotImplementedException(); }
        }


        public string DebugInfo
        {
            get { throw new NotImplementedException(); }
        }


        public float ChildOrdinal
        {
            get { throw new NotImplementedException(); }
        }


        public IRoot Root
        {
            get { throw new NotImplementedException(); }
        }
    }
}
